/**
 * 常用的时间方法
 */
function timeToymds(t) {
  const time = t ? new Date(t) : new Date()
  const year = time.getFullYear()
  let month = time.getMonth() + 1
  month = month < 10 ? '0' + month : month
  let day = time.getDate()
  day = day < 10 ? '0' + day : day
  let h = time.getHours()
  h = h < 10 ? '0' + h : h
  let m = time.getMinutes()
  m = m < 10 ? '0' + m : m
  let s = time.getSeconds()
  s = s < 10 ? '0' + s : s
  return `${year}/${month}/${day} ${h}:${m}:${s}`
}

function timeToymdhm(t) {
  const time = t ? new Date(t) : new Date()
  const year = time.getFullYear()
  let month = time.getMonth() + 1
  month = month < 10 ? '0' + month : month
  let day = time.getDate()
  day = day < 10 ? '0' + day : day
  let h = time.getHours()
  h = h < 10 ? '0' + h : h
  let m = time.getMinutes()
  m = m < 10 ? '0' + m : m
  // let s = time.getSeconds()
  // s = s < 10 ? '0' + s : s
  return `${year}-${month}-${day} ${h}:${m}`
}

function timeToymd(t) {
  const time = new Date(t)
  const year = time.getFullYear()
  let month = time.getMonth() + 1
  month = month < 10 ? '0' + month : month
  let day = time.getDate()
  day = day < 10 ? '0' + day : day
  return `${year}-${month}-${day}`
}
// 计算当前天数加指定天数
function getNewData(dateTemp, days) {
  dateTemp = dateTemp.split('/')
  const nDate = new Date(dateTemp[1] + '/' + dateTemp[2] + '/' + dateTemp[0]) // 转换为MM-DD-YYYY格式
  const millSeconds = Math.abs(nDate) + days * 24 * 60 * 60 * 1000
  const rDate = new Date(millSeconds)
  const year = rDate.getFullYear()
  let month = rDate.getMonth() + 1
  if (month < 10) month = '0' + month
  let date = rDate.getDate()
  if (date < 10) date = '0' + date
  let h = rDate.getHours()
  if (h < 10) h = '0' + h
  let m = rDate.getMinutes()
  if (m < 10) m = '0' + m
  let s = rDate.getSeconds()
  if (s < 10) s = '0' + s
  return year + '-' + month + '-' + date + ' ' + h + ':' + m + ':' + s
}
// 月份加减
function addMonths(date, monthNum) {
  let year = parseInt(date.getFullYear())
  let month = parseInt(date.getMonth() + 1)
  month = month + monthNum
  if (month > 12) {
    let yearNum = parseInt((month - 1) / 12)
    month = month % 12 == 0 ? 12 : month % 12
    year += yearNum
  } else if (month <= 0) {
    month = 12 - Math.abs(month)
    let yearNum = parseInt((month + 12) / 12)
    year -= yearNum
  }
  month = month < 10 ? '0' + month : month
  return year + '-' + month
}
// 获取当前日期
function getNewDateYMD() {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) month = '0' + month
  let day = date.getDate()
  if (day < 10) day = '0' + day
  return year + '/' + month + '/' + day
}
// 获取当前日期
function getNowDateYMD() {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) month = '0' + month
  let day = date.getDate()
  if (day < 10) day = '0' + day
  return year + '-' + month + '-' + day
}
// 获取当前日期
function getNowDateYM() {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) month = '0' + month
  return year + '-' + month
}
function getNewDateYMDHms() {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) month = '0' + month
  let day = date.getDate()
  if (day < 10) day = '0' + day
  let h = date.getHours()
  if (h < 10) h = '0' + h
  let m = date.getMinutes()
  if (m < 10) m = '0' + m
  let s = date.getSeconds()
  if (s < 10) s = '0' + s
  return year + '/' + month + '/' + day + ' ' + h + ':' + m + ':' + s
}
function getNewDateYMDHms1() {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) month = '0' + month
  let day = date.getDate()
  if (day < 10) day = '0' + day
  let h = date.getHours()
  if (h < 10) h = '0' + h
  let m = date.getMinutes()
  if (m < 10) m = '0' + m
  let s = date.getSeconds()
  if (s < 10) s = '0' + s
  return year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s
}
function getNewDateYMDH() {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) month = '0' + month
  let day = date.getDate()
  if (day < 10) day = '0' + day
  let h = date.getHours()
  if (h < 10) h = '0' + h
  return year + '-' + month + '-' + day + ' ' + h
}
// 格式化时间
function timeFormat(time) {
  // 时间格式化 2020/08/25
  const year = time.getFullYear()
  let month = time.getMonth() + 1
  if (month < 10) month = '0' + month
  let day = time.getDate()
  if (day < 10) day = '0' + day
  return year + '/' + month + '/' + day
}
// 格式化时间
function timeFormatHH(time) {
  // 时间格式化 2020/08/25
  const year = time.getFullYear()
  let month = time.getMonth() + 1
  if (month < 10) month = '0' + month
  let day = time.getDate()
  if (day < 10) day = '0' + day
  let h = time.getHours()
  if (h < 10) h = '0' + h
  let m = time.getMinutes()
  if (m < 10) m = '0' + m
  let s = time.getSeconds()
  if (s < 10) s = '0' + s
  return year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s
}
// 格式化时间
function timeFormatYMDH(time) {
  // 时间格式化 2020/08/25
  const year = time.getFullYear()
  let month = time.getMonth() + 1
  if (month < 10) month = '0' + month
  let day = time.getDate()
  if (day < 10) day = '0' + day
  let h = time.getHours()
  if (h < 10) h = '0' + h
  return year + '-' + month + '-' + day + ' ' + h
}
// 计算相差天数
function myGetDay(end) {
  end = end.replace(/-/g, '/')
  let days = 0
  let time = new Date().getTime()
  if (end) {
    // 转化日期
    time = new Date().getTime() - new Date(end).getTime()
    // 计算相差天数
    days = Math.floor(time / (24 * 3600 * 1000))
  }
  return days
}
// 计算两个时间相差天数
function dateDiff(sDate1, sDate2) {
  var startTime = new Date(Date.parse(sDate2.replace(/-/g, '/'))).getTime()
  var endTime = new Date(Date.parse(sDate1.replace(/-/g, '/'))).getTime()
  var dates = Math.abs(startTime - endTime) / (1000 * 60 * 60 * 24) + 1
  return dates
}
// 时间戳转日期
function	formatDate(date) {
			date = new Date(date);
			let myyear = date.getFullYear();
			let mymonth = date.getMonth() + 1;
			let myweekday = date.getDate();
			mymonth < 10 ? mymonth = "0" + mymonth : mymonth;
			myweekday < 10 ? myweekday = "0" + myweekday : myweekday;
			return `${myyear}-${mymonth}-${myweekday}`;
		}
// 获取某月第一天和最后一天日期
function	getDateByMonth(timeStamp) {
			let inDate = new Date(timeStamp)
			let year = inDate.getFullYear()
			let month = inDate.getMonth()
			let startTime = new Date(year, month, 1).getTime()
			let endTime = new Date(year, month + 1, 0).getTime()

			return {
				startTime: formatDate(startTime),
				endTime: formatDate(endTime)
			}
		}
export { timeToymds, timeToymdhm, timeToymd, getNewData, getNewDateYMD, getNowDateYMD, getNowDateYM, getNewDateYMDHms, getNewDateYMDHms1, getNewDateYMDH, timeFormat, timeFormatHH, timeFormatYMDH, myGetDay, dateDiff, addMonths, getDateByMonth }
